/**
 * Created with JetBrains WebStorm.
 * User: absir
 * Date: 14-11-10
 * Time: 下午4:18
 * To change this template use File | Settings | File Templates.
 */
auiModule.$navTab = function (id, ref, url, name, ext, $navTab) {
    var navTab = {
        id: id,
        ref: ref,
        url: url,
        name: name,
        ext: ext,
        body: '',
        $navTab: $navTab,
        close: function () {
            $navTab.remove(_id)
        }
    }

    function navigate(url, name) {
        if (url) {
            navTab.url = url;
        }

        if (name) {
            navTab.name = name;
        }

        auiModule.thenTemplate(function (template) {
            if (ext) {
                navTab.body = auiModule.$sce.trustAsHtml('<iframe src="' + navTab.url + '" style="width:100%;height:100%;" frameborder="no" border="0" marginwidth="0" marginheight="0"></iframe>');

            } else {
                navTab.body = auiModule.$sce.trustAsHtml(template);
                auiModule.$http.get(_url).success(function (response) {
                    navTab.body = auiModule.$sce.trustAsHtml(response);
                });
            }
        }, $navTab.loading);
    }

    navigate();
    navTab.refresh = navTab.navigate = navigate;
    return navTab;
};

auiModule.directive("aNavTab", function () {
    return {
        restrict: "EA",
        scope: true,
        link: function (scope, element, attrs) {
            var navTabs = scope.navTabs = [];
            var refNavTabs = {};
            $navTab = {
                loading: auiModule.tplSrc('loading.html', attrs.loadingTpl),
                refresh: function (index) {
                    navTabs[index].refresh();
                },
                remove: function (index) {
                    var navTab = navTabs[index];
                    navTabs.splice(index, 1);
                    if (navTab.ref) {
                        refNavTabs.remove(navTab.ref);
                    }
                },
                clear: function () {
                    navTabs.splice(0, navTabs.length);
                    refNavTabs = {};
                    $navTab.selected = null;
                }
            };

            $navTab.add = function (ref, url, name, ext) {
                var navTab;
                if (ref) {
                    if (ref.length < 1) {
                        ref = false;

                    } else {
                        navTab = refNavTabs[ref];
                    }
                }

                if (navTab) {
                    navTab.nav(url, name);

                } else {
                    navTab = auiModule.$navTab(navTabs.length, ref, url, name, ext, $navTab);
                    navTabs.push(navTab);
                    if (ref) {
                        refNavTabs[ref] = navTab;
                    }
                }
            };

            $navTab.select = function (index) {
                var selected = $navTab.selected;
                if (!selected || selected.index != index) {
                    $navTab.selected = navTabs[index];
                }
            };

            var aNavTab = attrs.aNavTab;
            if (aNavTab && aNavTab.length > 0) {
                auiModule.navTabs[aNavTab] = $navTab;

            } else {
                auiModule.navTab = $navTab;
            }
        }
    };
});

auiModule.directive("aRef", function () {
    return {
        restrict: "A",
        link: function (scope, element, attrs) {
            $(element).click(function (event) {
                var navTab = attrs.navTab;
                if (navTab) {
                    navTab = auiModule.navTabs[navTab];
                }

                if (!navTab) {
                    navTab = auiModule.navTab;
                }

                scope.$apply(function () {
                    navTab.add(attrs.aRef, attrs.href, attrs.title, attrs.aExt);
                });

                event.preventDefault();
                event.stopPropagation();
            });
        }
    }
});
